﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Xml.Utility;

namespace Portal.Cms.Model
{
    public class DocYearRangeCls
    {
        public string DocYearRangeId;
        public string DocYearRangeName;
        public int SortIndex = 0;
        public string frkGroupDataId;
        public int TotalItem = 0;
        public int FromYear = 0;
        public int ToYear = 0;
    }

    public class DocYearRangeParser
    {
        public static DocYearRangeCls CreateInstance()
        {
            //tao 1 class trang....
            DocYearRangeCls oDocYearRangeCls = new DocYearRangeCls();
            return oDocYearRangeCls;
        }

        public static DocYearRangeCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            DocYearRangeCls oDocYearRangeCls = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oDocYearRangeCls;
        }


        public static DocYearRangeCls ParseFromDataRow(DataRow dr)
        {
            DocYearRangeCls oDocYearRangeCls = new DocYearRangeCls();
            oDocYearRangeCls.DocYearRangeId = XmlUtility.GetString(dr, "DocYearRangeId", true);
            oDocYearRangeCls.frkGroupDataId = XmlUtility.GetString(dr, "frkGroupDataId", true);
            oDocYearRangeCls.DocYearRangeName = XmlUtility.GetString(dr, "DocYearRangeName", true);
            oDocYearRangeCls.SortIndex = XmlUtility.GetInt(dr, "SortIndex", true);
            oDocYearRangeCls.TotalItem = XmlUtility.GetInt(dr, "TotalItem", true);
            oDocYearRangeCls.FromYear = XmlUtility.GetInt(dr, "FromYear", true);
            oDocYearRangeCls.ToYear = XmlUtility.GetInt(dr, "ToYear", true);
            return oDocYearRangeCls;
        }

        public static DocYearRangeCls[] ParseFromDataTable(DataTable dtTable)
        {
            DocYearRangeCls[] DocYearRanges = new DocYearRangeCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                DocYearRanges[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return DocYearRanges;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet();
            ds.Tables.Add("Info");

            ds.Tables["Info"].Columns.Add("DocYearRangeId");
            ds.Tables["Info"].Columns.Add("frkGroupDataId");
            ds.Tables["Info"].Columns.Add("DocYearRangeName");
            ds.Tables["Info"].Columns.Add("SortIndex");
            ds.Tables["Info"].Columns.Add("TotalItem");
            ds.Tables["Info"].Columns.Add("FromYear");
            ds.Tables["Info"].Columns.Add("ToYear");
            return ds;
        }

        public static string GetXml(DocYearRangeCls oDocYearRangeCls)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                oDocYearRangeCls.DocYearRangeId,
                oDocYearRangeCls.frkGroupDataId,
                oDocYearRangeCls.DocYearRangeName,
                oDocYearRangeCls.SortIndex,
                oDocYearRangeCls.TotalItem,
                oDocYearRangeCls.FromYear,
                oDocYearRangeCls.ToYear
            });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            DocYearRangeCls oDocYearRange = DocYearRangeParser.ParseFromDataRow(dr);
            string Xml = DocYearRangeParser.GetXml(oDocYearRange);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            DocYearRangeCls[] DocYearRanges = new DocYearRangeCls[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                DocYearRanges[iIndex] = DocYearRangeParser.ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = DocYearRangeParser.GetXml(DocYearRanges);
            return Xml;
        }


        public static string GetXml(DocYearRangeCls[] DocYearRanges)
        {
            if (DocYearRanges == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                DocYearRanges = new DocYearRangeCls[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < DocYearRanges.Length; iIndex++)
            {
                DocYearRangeCls oDocYearRangeCls = DocYearRanges[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                {
                    oDocYearRangeCls.DocYearRangeId,
                    oDocYearRangeCls.frkGroupDataId,
                    oDocYearRangeCls.DocYearRangeName,
                    oDocYearRangeCls.SortIndex,
                    oDocYearRangeCls.TotalItem,
                    oDocYearRangeCls.FromYear,
                    oDocYearRangeCls.ToYear,
                });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static DocYearRangeCls[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new DocYearRangeCls[0];
            }
            DocYearRangeCls[] DocYearRanges = new DocYearRangeCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                DocYearRanges[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return DocYearRanges;
        }
    }
}
